package grape.house.rest.buildingroom.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.house.rest.buildingroom.form.BuildingRoomListForm;
import grape.house.service.building.api.IBuildingService;
import grape.house.service.building.po.Building;
import grape.house.service.buildingroom.query.BuildingRoomListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.house.rest.buildingroom.form.BuildingRoomCreateForm;
import grape.house.rest.buildingroom.form.BuildingRoomUpdateForm;
import grape.house.rest.buildingroom.form.BuildingRoomListPageForm;
import grape.house.rest.buildingroom.vo.BuildingRoomVo;
import grape.house.rest.buildingroom.mapper.BuildingRoomWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.house.service.buildingroom.po.BuildingRoom;
import grape.house.service.buildingroom.api.IBuildingRoomService;
import java.util.List;
/**
 * <p>
 * 房屋 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/house/buildingroom")
@Api(tags = "房屋相关接口")
public class BuildingRoomController extends BaseController<BuildingRoomVo, BuildingRoom> {

    @Autowired
    private BuildingRoomWebMapper currentWebMapper;
    @Autowired
    private IBuildingRoomService currentService;
    @Autowired
    private IBuildingService iBuildingService;



     @ApiOperation("添加房屋")
     @PreAuthorize("hasAuthority('buildingroom:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public BuildingRoomVo create(@RequestBody @Valid BuildingRoomCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }

         BuildingRoom po = currentWebMapper.formToPo(cf);
         // 建筑面积 = 使用面积 + 分摊面积
         po.setBuildArea(po.getUsableArea() + po.getShareArea());
         Building byId = iBuildingService.getById(po.getBuildingId());
         po.setEstateId(byId.getEstateId());
         return super.create(po);
     }

     @ApiOperation("根据ID查询房屋")
     @PreAuthorize("hasAuthority('buildingroom:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public BuildingRoomVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除房屋")
     @PreAuthorize("hasAuthority('buildingroom:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新房屋")
     @PreAuthorize("hasAuthority('buildingroom:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public BuildingRoomVo update(@PathVariable String id,@RequestBody @Valid BuildingRoomUpdateForm uf) {
         BuildingRoom po = currentWebMapper.formToPo(uf);
         // 建筑面积 = 使用面积 + 分摊面积
         po.setBuildArea(po.getUsableArea() + po.getShareArea());
         Building byId = iBuildingService.getById(po.getBuildingId());
         po.setEstateId(byId.getEstateId());
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询房屋")
    @PreAuthorize("hasAuthority('buildingroom:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<BuildingRoomVo> list(BuildingRoomListForm listForm) {
        BuildingRoom po = currentWebMapper.formToPo(listForm);
        BuildingRoomListQuery buildingRoomListQuery = new BuildingRoomListQuery();
        buildingRoomListQuery.setPoQuery(po);
        return super.list(buildingRoomListQuery);
    }

    @ApiOperation("分页查询房屋")
    @PreAuthorize("hasAuthority('buildingroom:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<BuildingRoomVo> listPage(BuildingRoomListPageForm listPageForm) {
         BuildingRoom po = currentWebMapper.formToPo(listPageForm);
        BuildingRoomListQuery buildingRoomListQuery = new BuildingRoomListQuery();
        buildingRoomListQuery.setPoQuery(po);
         return super.listPage(buildingRoomListQuery,listPageForm);
     }

}
